package missPuff.com;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import model.com.VideoInfo;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class VideoListAdapter extends BaseAdapter {
	private Map<Integer, View> rowViews = new HashMap<Integer, View>();
	private List<VideoInfo> list;
	private Context context;
	private File file;

	public VideoListAdapter(Context context, List<VideoInfo> list) {
		this.context = context;
		this.list = list;
	}
	
	public void changeList(List<VideoInfo> list){
		this.list = list;
	}

	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View rowView = rowViews.get(position);
		if (rowView == null) {
			LayoutInflater layoutInflater = LayoutInflater.from(context);
			rowView = layoutInflater.inflate(R.layout.videolist_item, null);
			ImageView video_image = (ImageView) rowView
					.findViewById(R.id.video_image);
			TextView video_title = (TextView) rowView
					.findViewById(R.id.video_title);
			TextView video_time = (TextView) rowView
					.findViewById(R.id.video_time);

			file = new File(list.get(position).getImage_localUrl());
			video_title.setText(list.get(position).getVideo_title());
			video_time.setText(list.get(position).getVideo_time());
			if(file.exists()){
				video_image.setImageBitmap(BitmapFactory.decodeFile(list.get(position).getImage_localUrl()));
			}
			else{
				video_image.setImageResource(R.drawable.video_pic);
			}
		}

		return rowView;
	}	
}
